package com.hc.summary.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class CircleIndicator extends View {
    private int mIndicatorWidth;
    private int mIndicatorCount;
    private int mSelectedPosition;
    private float mCy;
    private float mSelectedRadius;
    private float mUnSelectedRadius;
    private Paint mSelectedPaint;
    private Paint mUnSelectedPaint;
    private List<Rect> mItemCenterList;
    private IndicatorClickListener mListener;

    public CircleIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mSelectedPaint = new Paint();
        mSelectedPaint.setStyle(Paint.Style.FILL);
        mSelectedPaint.setAntiAlias(true);
        mSelectedPaint.setColor(Color.WHITE);
        mUnSelectedPaint = new Paint();
        mUnSelectedPaint.setStyle(Paint.Style.FILL);
        mUnSelectedPaint.setAntiAlias(true);
        mUnSelectedPaint.setColor(Color.GRAY);
        mItemCenterList = new ArrayList<>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        mIndicatorWidth = width / (mIndicatorCount + mIndicatorCount - 1);
        int indicatorHeight = Math.min(mIndicatorWidth, height);
        if(indicatorHeight < height){
            indicatorHeight = height;
        }
        mSelectedRadius = mIndicatorWidth / 2f;
        mUnSelectedRadius = mSelectedRadius / 1.5f;
        mCy = indicatorHeight / 2f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mIndicatorCount; i++) {
            float cx = i * 2 * mIndicatorWidth + mIndicatorWidth / 2f;
            if (mIndicatorCount > 1 && mItemCenterList.size() < mIndicatorCount) {
                Rect rect = new Rect();
                rect.set((int) (cx - mSelectedRadius), (int) (mCy - mSelectedRadius), (int) (cx + mSelectedRadius), (int) (mCy + mSelectedRadius));
                mItemCenterList.add(rect);
            }
            if (i == mSelectedPosition) {
                canvas.drawCircle(cx, mCy, mSelectedRadius, mSelectedPaint);
            } else {
                canvas.drawCircle(cx, mCy, mUnSelectedRadius, mUnSelectedPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            float x = event.getX();
            float y = event.getY();
            for (int i = 0; i < mItemCenterList.size(); i++) {
                Rect rect = mItemCenterList.get(i);
                if (rect.contains((int) x, (int) y) && mListener != null) {
                    mSelectedPosition = i;
                    mListener.onClick(i);
                    invalidate();
                    return true;
                }
            }
        }
        return true;
    }

    public interface IndicatorClickListener {
        void onClick(int position);
    }

    public void setListener(IndicatorClickListener listener) {
        this.mListener = listener;
    }

    public void setIndicatorCount(int count) {
        mItemCenterList.clear();
        mIndicatorCount = count;
        if (mSelectedPosition >= mIndicatorCount) {
            mSelectedPosition = mIndicatorCount - 1;
        }
        if (mIndicatorCount <= 1) {
            setVisibility(GONE);
        } else {
            setVisibility(VISIBLE);
        }
    }

    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
        invalidate();
    }
}
